package com.wormpex.data.root.service.impl;


import com.wormpex.data.root.dao.FailedTransmitMessageDao;
import com.wormpex.data.root.model.bean.FailedTransmitMessage;
import com.wormpex.data.root.service.FailedTransmitMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by liangzuo.wang on 17/8/18
 * 失败消息重试
 */

@Service
public class FailedTransmitMessageServiceImpl implements FailedTransmitMessageService {

    private final static Logger LOG = LoggerFactory.getLogger(FailedTransmitMessageServiceImpl.class);

    @Autowired
    private FailedTransmitMessageDao failedTransmitMessageDao;

    @Autowired
    private WmqTransmitKafkaServiceImpl wmqTransmitKafkaServiceImpl;

    @Override
    public FailedTransmitMessage getById(long id) {
        return failedTransmitMessageDao.selectByPrimaryKey(id);
    }

    @Override
    public void insert(FailedTransmitMessage failedTransmitMessage) {
        failedTransmitMessageDao.insertSelective(failedTransmitMessage);
    }

    @Override
    public void update(FailedTransmitMessage failedTransmitMessage) {
        failedTransmitMessageDao.updateByPrimaryKeySelective(failedTransmitMessage);
    }

    @Override
    public boolean rerunMsg(long id) {
        FailedTransmitMessage failedTransmitMessage = getById(id);
        if(failedTransmitMessage == null)
            return false;
        boolean processResult = wmqTransmitKafkaServiceImpl.transmit1(failedTransmitMessage.getToTopicName(),failedTransmitMessage.getMessage());
        if(processResult) {
            failedTransmitMessage.setSendStatus(true);
            Integer newRetryTimes = failedTransmitMessage.getRetryTimes() + 1;
            failedTransmitMessage.setRetryTimes(newRetryTimes);
        }
        failedTransmitMessageDao.updateByPrimaryKeySelective(failedTransmitMessage);
        return processResult;
    }

    @Override
    public boolean rerunMsg(FailedTransmitMessage failedTransmitMessage){

        boolean processResult = false;
        if(failedTransmitMessage.getFromSourceType().equals("Wmq")
                && failedTransmitMessage.getToSourceType().equals("Kafka")){
            processResult = wmqTransmitKafkaServiceImpl.transmit1(failedTransmitMessage.getToTopicName(), failedTransmitMessage.getMessage());
        }
        return processResult;

    }

    @Override
    public void rerunMsgs() {
        List<FailedTransmitMessage> failedTransmitMessages = failedTransmitMessageDao.getFailedTransmitMessages();
        for(FailedTransmitMessage failedTransmitMessage : failedTransmitMessages) {
            if( rerunMsg(failedTransmitMessage) ) {
                failedTransmitMessage.setSendStatus(true);
            }
            Integer newRetryTimes = failedTransmitMessage.getRetryTimes() + 1;
            failedTransmitMessage.setRetryTimes(newRetryTimes);
            failedTransmitMessageDao.updateByPrimaryKeySelective(failedTransmitMessage);
        }
    }
}
